package com.itheima.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.PageResult;
import com.itheima.domain.PmsProductCategory;
import com.itheima.mapper.PmsProductCategoryMapper;
import com.itheima.service.PmsProductCategoryService;
import com.itheima.vo.PmsProductCategoryWithChildrenItem;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 产品分类 服务实现类
 * </p>
 *
 * @author zhuguo
 * @since 2022-11-10
 */
@DubboService
public class PmsProductCategoryServiceImpl extends ServiceImpl<PmsProductCategoryMapper, PmsProductCategory> implements PmsProductCategoryService {

    @Autowired
    PmsProductCategoryMapper productCategoryMapper;

    @Override
    public PageResult pageByParentId(Integer parentId, Integer pageNum, Integer pageSize) {
        Page<PmsProductCategory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<PmsProductCategory> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StrUtil.isNotBlank(parentId + ""), PmsProductCategory::getParentId, parentId);
        page(page, lqw);
        return new PageResult(pageNum, pageSize, page.getTotal(), page.getPages(), page.getRecords());
    }

//    @Override
//    public List<PmsProductCategoryWithChildrenItem> getWithChildren() {
//        //查询所有分类数据
//        List<PmsProductCategory> list1 = this.list();
//
//        //封装
//        List<PmsProductCategoryWithChildrenItem> listWithc = list1.stream().map(item -> {
//            PmsProductCategoryWithChildrenItem childrenItem = new PmsProductCategoryWithChildrenItem();
//            BeanUtils.copyProperties(item, childrenItem);
//            return childrenItem;
//        }).collect(Collectors.toList());
//
//
//        List<PmsProductCategoryWithChildrenItem> list2 = listWithc.stream().filter(childrenItem ->
//                childrenItem.getParentId() == 0
//        ).map((menu) -> {
//            menu.setChildren(getChildrenData(menu, listWithc));
//            return menu;
//        }).collect(Collectors.toList());
//        return list2;
//    }

    @Override
    public List<PmsProductCategory> findAll() {

        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectList(null);
        return pmsProductCategories;

    }

//    //递归查询子类的方法
//    private List<PmsProductCategoryWithChildrenItem> getChildrenData(PmsProductCategoryWithChildrenItem menu, List<PmsProductCategoryWithChildrenItem> all) {
//        List<PmsProductCategoryWithChildrenItem> children = all.stream().filter(Ppcwc ->
//                Ppcwc.getParentId().equals(menu.getId())
//        ).map(Ppcwc -> {
//            Ppcwc.setChildren(getChildrenData(Ppcwc, all));
//            return Ppcwc;
//        }).collect(Collectors.toList());
//        return children;
//    }
}
